package com.example.qixin0314.bean;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qixin0314.R;

import java.util.List;

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ContacHolder>{

    private Context context;

    public List<ChangeModel> data;

    private onItemViewClickListener listener;

    public SearchAdapter(Context context){

        this.context = context;

    }

    public void setData(List<ChangeModel> data){
        this.data = data;
    }

    public onItemViewClickListener getListener() {
        return listener;
    }

    public void setListener(onItemViewClickListener listener) {
        this.listener = listener;
    }

    //创建ViewHolder对象@NonNull
    @NonNull
    @Override
    public ContacHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        return new ContacHolder(LayoutInflater.from(context). inflate(R.layout.list_home_change,null));
    }

    @Override
    public void onBindViewHolder(@NonNull ContacHolder holder,int position) {

        //建立联系数据List与条目布局之间
        holder.searchName.setText(data.get(position).getTitle());//标题

        holder.searchName.setOnClickListener(new View.OnClickListener() {//文字单击
            @Override
            public void onClick(View view) {
                    listener.onTextClick(position);
            }
        });

    }

    //同BaseAdapter.getCount()
    @Override
    public int getItemCount(){
        return null != data ? data.size() : 0;
    }

    //由实现的父类构造方法可以得出
    //ViewHolder被new的时候，需要传入条目布局转换后的View对象
    class ContacHolder extends RecyclerView.ViewHolder {

        TextView searchName;//标题

        public ContacHolder(@NonNull  View itemView) {
            super(itemView);
            searchName = itemView.findViewById(R.id.change_name);//标题
        }
    }

    public interface onItemViewClickListener{
        void onTextClick(int position);//文字单击
    }

}
